<?php

class SystemController extends Zend_Controller_Action
{

    public function init()
    {
		/* Set title */
		$this->view->headTitle("Hệ thống");
    }

    public function indexAction()
    {
        // action body
    }

    public function permissionAction()
    {
        // action body
        if($data_ajax = $this->_request->getParam('ajax',null)){
            list($manhom,$manghiepvu) = $data_ajax;
            $model = new Application_Model_DbTable_TrachNhiem();
            $select = $model->select()->where("MaNhom=?",$manhom)
                                      ->where("MaNghiepVu=?",$manghiepvu);
            $row = $model->fetchRow( $select );
            if($row){
                $row->ChoPhep = ($row->ChoPhep==1)?0:1;
                $row->save(); 
            }
            return $this->_helper->json->sendJson($row->toArray());
        }
        
        $model = new Application_Model_DbTable_TrachNhiem();
        $rows = $model->getDataPermission();
        $this->view->datas = array();
        $this->view->ds_tennghiepvu = array();
        $this->view->ds_tennhom = array();
        foreach($rows as $row){
            $this->view->datas[$row->MaNghiepVu][$row->MaNhom] = $row->ChoPhep;
            $this->view->ds_tennghiepvu[$row->MaNghiepVu] = $row->TenNghiepVu;
            $this->view->ds_tennhom[$row->MaNhom] = $row->TenNhom;
        }
    }

    public function configAction()
    {
        // action body
        $form = new Application_Form_Config();
        $model = new Application_Model_DbTable_ThamSo();
        $row = $model->find(1)->current();
        if(!$row){
            $row = $model->createRow(array('MaThamSo'=> 1));
            $row->save();
        }
        $form->populate($row->toArray());
        if($this->_request->isPost()){
            if($form->isValid($_POST)){
                $datas = $form->getValues();
                foreach($datas as $key=>$value){
                    $row->{$key} = $value;
                }
                $row->save();
                $this->view->message = "Đã cập nhật dữ liệu.";
            }
        }
        $this->view->form = $form;
    }

    public function groupAction()
    {
        // action body
        $model = new Application_Model_DbTable_NhomTaiKhoan();
        
        if( $this->_request->isPost() ){
            $datas = $this->_request->getParams();
            
            $result = $model->{$datas['ajax_action']}( $datas['sdata'] );
                
            return $this->_helper->json->sendJson( $result );
        }
        
        $this->view->datas = $model->fetchAll();
    }

    public function accountAction()
    {
        // action body
        $nhom = new Application_Model_DbTable_NhomTaiKhoan();
        $taikhoan = new Application_Model_DbTable_TaiKhoan();
        
        if($this->_request->isPost()) {
            $datas = $this->_request->getParams();
            $result = $taikhoan->{$datas['ajax_action']}( $datas['sdata'] );                
            return $this->_helper->json->sendJson( $result );
        }
        $this->view->ds_nhom = $nhom->getDatas();
    }


}